package com.lvjunxiong.serviceinterface.tongxun;

import common.CommModel;
import common.database.DBManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lvjunxiong on 2019/2/28.
 */
public class TongxunDao {

    public List<TongxunModel> queryGroupName() {
            List<TongxunModel> list = new ArrayList<TongxunModel>();
            DBManager dbm = null;
            try {
                dbm = new DBManager();
                String sql = "select groupName as groupName from sh_tongxun order by id";
                list = dbm.getObjectList(TongxunModel.class,sql);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                dbm.close();
            }
            return list;
    }

    public int insertSh_suoyin(String groupName) {
        int i = 0;
        DBManager dbm = null;
        try {
            dbm = new DBManager();
            String sql = "insert into sh_suoyin(id,enabled) values('"+ groupName +"','');";
            i = dbm.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbm.close();
        }
        return i;
    }

    public int insertSh_suoyin_list(String gain_id1, String gain_name1, String gain_name11,String type1) {
        int i = 0;
        DBManager dbm = null;
        try {
            dbm = new DBManager();
            String sql =  "insert into sh_suoyin_list(id_s,id,name,company,type,datatype) values('" + gain_id1 + "','"+ gain_name11 +"','"+ gain_name1 +"','','"+  type1+"','I')";
            i = dbm.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbm.close();
        }
        return i;
    }

    public List<CommModel> queryYB(String groupName) {
        List<CommModel> list = new ArrayList<CommModel>();
        DBManager dbm = null;
        try {
            dbm = new DBManager();
            String sql = "select name as result from sh_suoyin_list where id = '"+ groupName +"'";
            list = dbm.getObjectList(CommModel.class,sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbm.close();
        }
        return list;
    }

    public int updatesh_suoyin(String groupName,String str) {
        int i = 0;
        DBManager dbm = null;
        try {
            dbm = new DBManager();
            String sql = "update sh_suoyin set enabled = '"+ str +"' where id = '"+ groupName +"'";
            i = dbm.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbm.close();
        }
        return i;
    }

    public int updatesh_suoyinList(String gain_id1, String company_name) {
        int i = 0;
        DBManager dbm = null;
        try {
            dbm = new DBManager();
            String sql = "update sh_suoyin_list set company = '" + company_name + "' where id_s = '" + gain_id1 + "'";
            i = dbm.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbm.close();
        }
        return i;
    }

    public int delOther() {
        int i = 0;
        DBManager dbm = null;
        try {
            dbm = new DBManager();
            String sql = "delete from sh_suoyin_list where type = 'PARTY_GROUP' or type = 'DEPARTMENT' or type = 'CORPORATION'";
            i = dbm.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbm.close();
        }
        return i;
    }
}
